package baiguoyuan.kafka.simple;

import kafka.utils.ShutdownableThread;
import org.apache.kafka.clients.consumer.*;
import java.util.*;


public class KafkaConsumerTest1  extends ShutdownableThread {
    private final KafkaConsumer<String, String> consumer;
    private final String topic;
    public KafkaConsumerTest1(String topic) {
        super("KafkaConsumerExample", false);
        this.topic = topic;
        Properties props = new Properties();
        props.put("bootstrap.servers","10.8.39.3:9092,10.8.39.13:9092,10.8.39.10:9092");
        props.put("group.id","groupG");
        props.put("enable.auto.commit", "true");
        props.put("auto.commit.interval.ms", "1000");
        props.put("session.timeout.ms", "30000");
        props.put("auto.offset.reset", "earliest");
//        props.put("max.poll.records", 20);//设置最新拉取数据条数
        props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        consumer = new KafkaConsumer<>(props);
    }

    @Override
    public void doWork() {
        consumer.subscribe(Collections.singletonList(topic));
        ConsumerRecords<String, String> records = consumer.poll(1000);
        for (ConsumerRecord<String, String> record : records) {
            System.out.println(record.offset() + "     " + record.key() + "    " + record.value());
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

    @Override
    public String name() {
        return null;
    }

    @Override
    public boolean isInterruptible() {
        return false;
    }

    public static void main(String args[]) {
        KafkaConsumerTest1 test1 = new KafkaConsumerTest1("sal_order");
        test1.start();
    }

}
